/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author thynk
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

public class AddProductDialog extends JDialog {
    private JPanel bottom;
    private JButton okButton, cancelButton;
    private AddProductPanel dialogPanel;
    private final int Width = 500;
    private final int Height = 300;

    public AddProductDialog(JFrame owner) {
        super(owner, true);
        setTitle("Add New Product");

        okButton = new JButton("OK");
        cancelButton = new JButton("Cancel");
        ButtonHandler handler = new ButtonHandler();
        okButton.addActionListener(handler);
        cancelButton.addActionListener(handler);

        bottom = new JPanel();
        bottom.add(okButton);
        bottom.add(cancelButton);
        bottom.setBorder(BorderFactory.createEtchedBorder());
        dialogPanel = new AddProductPanel();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(bottom, BorderLayout.SOUTH);
        getContentPane().add(dialogPanel, BorderLayout.CENTER);

        setSize(Width,Height);
    }

    class ButtonHandler implements ActionListener {
        public void actionPerformed (ActionEvent evt) {
            JButton button = (JButton) evt.getSource();
            String label = button.getText();

            if("OK".equals(label)) {
                // add new product into product list
                setProduct();
            }
            
            dialogPanel.reset();
            setVisible(false);
        }
    }

    /* set new product */
    private void setProduct() {
        String productName = dialogPanel.nameField.getText();   // get name of new product
        //temp.setnameProduct(productName);   // set name of new product
        int num = Integer.parseInt(dialogPanel.quantityField.getText()); // get quantity
        //temp.setinvOnHand(num); // set inventory on hand of new product
        //int numRM = dialogPanel.getInventory().size();  // get number of inventory needed for new productv
        ArrayList invName = dialogPanel.getInventory();
        ArrayList invNum = dialogPanel.getNum();

        // set product
        Product newProduct = new Product(productName, invName.size());
        newProduct.setInvOnHand(num);

        ArrayList<Ingredients> invTemp = new  ArrayList<Ingredients>();
        ArrayList numNeeded = new ArrayList();

        // set inventory for a product
        for (int i = 0; i < invName.size(); ++i) {
            String st = (String) invName.get(i);    // get name of inventory
            Ingredients tmp = (Ingredients) SupplyChainManagement.inv.get(SupplyChainManagement.checkDup(st));  // get inventory
            st = (String) invNum.get(i);
            num = Integer.parseInt(st);
            invTemp.add(tmp);
            numNeeded.add(num);
            //newProduct.setRawMeterials(tmp, num);
        }
        newProduct.setRawMeterials(invTemp, numNeeded);
        // add new product into product list
        SupplyChainManagement.p.add(newProduct);
        SupplyChainManagement.writeProduct();
    }

}
